package logic

import (
	"context"
	"shop/cache"
	"strconv"

	"shop/api/internal/svc"
	"shop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCartDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartDeleteLogic {
	return &CartDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CartDeleteLogic) CartDelete(req *types.CartDeleteRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userIdStr, ok := l.ctx.Value("user_id").(string)
	if !ok {
		return &types.Response{
			Code:    401,
			Message: "未授权",
			Data:    nil,
		}, nil
	}
	userID, _ := strconv.Atoi(userIdStr)

	err = cache.CartDelete(int64(userID), req.GoodsID)
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: "删除失败",
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "删除成功",
		Data:    nil,
	}, nil
}
